package look4ss.aplicativo;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.TextInputLayout;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;

// a função de recuperação de senhas foi suspensa indeterminadamente ## 23/09/2018
public class ActivityRecuperarSenha extends AppCompatActivity {

    private EditText enderecoEmail;
    private Button resetaSenha;
    private TextView voltarTelaLogin;
    private TextInputLayout txtEmail;
    private ProgressBar barraDeLoading;
    private FirebaseAuth firebaseAuth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recuperar_senha);

        enderecoEmail = findViewById(R.id.endereco_de_email);
        resetaSenha = findViewById(R.id.resetarsenha);
        voltarTelaLogin = findViewById(R.id.voltar_ao_login);
        txtEmail = findViewById(R.id.layout_entrada_texto);
        barraDeLoading  = findViewById(R.id.barraLoading);
        voltarTelaLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(ActivityRecuperarSenha.this, ActivityLogin.class));
            }
        });
        firebaseAuth = FirebaseAuth.getInstance();

        resetaSenha.setTextColor(ContextCompat.getColor(ActivityRecuperarSenha.this, R.color.textoBotoes));
        resetaSenha.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String resetEmail = enderecoEmail.getText().toString();
                if(TextUtils.isEmpty(resetEmail))
                {
                    txtEmail.setError("DIGITE UM EMAIL");
                }
                barraDeLoading.setVisibility(View.VISIBLE);
                firebaseAuth.sendPasswordResetEmail(resetEmail).addOnCompleteListener(ActivityRecuperarSenha.this, new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {

                        barraDeLoading.setVisibility(View.GONE);

                        if (task.isSuccessful()) {
                            Toast.makeText(ActivityRecuperarSenha.this, "Verifique seu e-mail", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(ActivityRecuperarSenha.this, "Erro ao enviar e-mail para recuperação de senha", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        });
    }
}